<template>
    <el-card shadow="never" :header="entityObj.title">
        <el-form
            ref="dialogForm"
            :rules="rules"
            :model="entityObj"
            label-width="120px"
            style="margin-top: 20px"
        >
            <el-form-item :label="titleDetail.orgCode">
                <el-input v-model="entityObj.orgCode" disabled></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.orgName">
                <el-input v-model="entityObj.orgName" ></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.orgFullName">
                <el-input v-model="entityObj.orgFullName" ></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.electronicAccount">
                <el-input v-model="entityObj.electronicAccount" disabled></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.balance">
                <el-input v-model="entityObj.balance" disabled></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.name">
                <el-input v-model="entityObj.name"></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.mobile">
                <el-input v-model="entityObj.mobile"></el-input>
            </el-form-item>
            <el-form-item :label="titleDetail.address">
                <el-input v-model="entityObj.address"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit" :disabled="isSaveing">保存</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import bus from "@/utils/mitt";
export default {
    data() {
        return {
            mode: "edit",
            userCode: "",
            titleDetail: {
                title: "机构信息",
                orgCode: "机构编码",
                orgName: "机构名称",
                orgFullName: "机构全称",
                electronicAccount:"电子账户",
                balance:"余额",
                name: "联系人",
                mobile: "手机号",
                address: "地址",
            },
            entityObj: {
                title: "机构信息",
                orgCode: "管理员",
                orgName: "管理员",
                name: "Sakuya",
                mobile: "13888888888",
                address: "administrator@scuiadmin.com",
            },
            rules: {
                Name: [{ required: true, type: "string", message: "请填写" }],
                Mobile: [{ required: true, type: "string", message: "请填写" }],
                WeChat: [{ required: true, type: "string", message: "请填写" }],
            },
            saveState: false,
            isSaveing: false,
            orgType: "",
        };
    },
    methods: {
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    switch (this.orgType) {
                        case 1:
                            var returnValue =
                                await this.$API.auth.PostProcessApi.post(
                                    "purchase-unity/update-current",
                                    {
                                        orgName: this.entityObj.orgName,
                                        orgFullName: this.entityObj.orgFullName,
                                        realName: this.entityObj.name,
                                        mobile: this.entityObj.mobile,
                                        address: this.entityObj.address,
                                    }
                                );
                            if (returnValue.status == 200) {
                                this.$message.success("操作成功");
                                this.isSaveing = false;
                            } else {
                                this.$message.error(
                                    "操作失败:" + returnValue.msg
                                );
                                this.isSaveing = false;
                            }

                            break;
                        case 2:
                            var returnValue =
                                await this.$API.auth.PostProcessApi.post(
                                    "store/update-current",
                                    {
                                        orgName: this.entityObj.orgName,
                                        orgFullName: this.entityObj.orgFullName,
                                        realName: this.entityObj.name,
                                        mobile: this.entityObj.mobile,
                                        address: this.entityObj.address,
                                    }
                                );
                            if (returnValue.status == 200) {
                                this.$message.success("操作成功");
                                this.isSaveing = false;
                            } else {
                                this.$message.error(
                                    "操作失败:" + returnValue.msg
                                );
                                this.isSaveing = false;
                            }
                            break;
                        case 3:
                            var returnValue =
                                await this.$API.auth.PostProcessApi.post(
                                    "supplier/update-current",
                                    {
                                        orgName: this.entityObj.orgName,
                                        orgFullName: this.entityObj.orgFullName,
                                        realName: this.entityObj.name,
                                        mobile: this.entityObj.mobile,
                                        address: this.entityObj.address,
                                    }
                                );
                            if (returnValue.status == 200) {
                                this.$message.success("操作成功");
                                this.isSaveing = false;
                            } else {
                                this.$message.error(
                                    "操作失败:" + returnValue.msg
                                );
                                this.isSaveing = false;
                            }
                            break;
                    }
                }
            });
        },

        //表单注入数据
        async setData() {
            this.orgType = tool.data.get("USER_INFO").AccountType;
            switch (this.orgType) {
                case 1:
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "purchase-unity/current",
                                {}
                            );
                        if (returnValue.status == 200) {
                            const response = returnValue.response;
                            this.entityObj.orgCode = response.purchaseUnityCode;
                            this.entityObj.orgName = response.purchaseUnityName;
                            this.entityObj.orgFullName = response.purchaseUnityFullName;
                            this.entityObj.electronicAccount = response.electronicAccount;
                            this.entityObj.balance = response.balance;
                            this.entityObj.name = response.realName;
                            this.entityObj.mobile = response.mobile;
                            this.entityObj.address = response.address;
                        }
                    } catch (error) {
                        return false;
                    }
                    break;
                case 2:
                    var returnValue = await this.$API.auth.PostProcessApi.post(
                        "store/current",
                        {}
                    );
                    if (returnValue.status == 200) {
                        const response = returnValue.response;
                        this.entityObj.orgCode = response.storeCode;
                        this.entityObj.orgName = response.storeName;
                        this.entityObj.orgFullName = response.storeFullName;
                        this.entityObj.electronicAccount = response.electronicAccount;
                        this.entityObj.balance = response.balance;
                        this.entityObj.name = response.realName;
                        this.entityObj.mobile = response.mobile;
                        this.entityObj.address = response.address;
                    }
                    break;
                case 3:
                    var returnValue = await this.$API.auth.PostProcessApi.post(
                        "supplier/current",
                        {}
                    );
                    if (returnValue.status == 200) {
                        const response = returnValue.response;
                        this.entityObj.orgCode = response.supplierCode;
                        this.entityObj.orgName = response.supplierName;
                        this.entityObj.orgFullName = response.supplierFullName;
                        this.entityObj.electronicAccount = response.electronicAccount;
                        this.entityObj.balance = response.balance;
                        this.entityObj.name = response.realName;
                        this.entityObj.mobile = response.mobile;
                        this.entityObj.address = response.address;
                    }
                    break;
            }
        },
    },
    async mounted() {
        await this.setData();
    },
};
</script>

<style></style>